# -*- coding: utf-8 -*-
"""
@Time ： 2023/9/10 20:55
@Auth ： y.h
@File ：save_file.py
@IDE ：PyCharm
@Motto：The sooner you start to code, the longer the program will take. —— Roy Carlson
"""

# todo openpyxl可以直接存一维数组,存不了二维数组


import tkinter
from tkinter import messagebox

import pandas

from pythonProjectTemplate.Lib.site_packages.openpyxl import Workbook, load_workbook

__all__ = ["save_file", "create_sheet_save", "create_workbook_save", "remove_sheet", "move_sheet"]


def save_file(file_name: str, sheet_name: str, data_list) -> bool:
    """
    在原工作表追加写入
    :param file_name: 资源路径
    :type file_name: str
    :param sheet_name: 表名称
    :type sheet_name: str
    :param data_list: 数据二维数组
    :type data_list: list[list[]]
    :return: 保存成功 ? true : false
    :rtype: bool
    """
    flag = False
    try:
        wb = load_workbook(filename=file_name)
        ws = wb[sheet_name]
        for data in data_list:
            ws.append(data)
        wb.save()
        flag = True
    except Exception as e:
        root = tkinter.Tk()
        root.withdraw()
        messagebox.showerror("文件存储错误", str(e))
    finally:
        return flag


def create_sheet_save(file_name: str, sheet_name: str, title_list,
                      save_data) -> bool:
    """
    新建工作表写入
    :param title_list:
    :type title_list:
    :param save_data:数据二维数组
    :type save_data:list[list[]]
    :param file_name:资源路径
    :type file_name:str
    :param sheet_name:表名称
    :type sheet_name:str
    :return:保存成功 ? true : false
    :rtype:bool
    """
    flag = False
    try:
        wb = load_workbook(filename=file_name)
        ws = wb.create_sheet(sheet_name)
        # 写入title
        ws.append(title_list)
        for data_list in save_data:
            ws.append(data_list)

        wb.save(file_name)
        flag = True
    except Exception as e:
        root = tkinter.Tk()
        root.withdraw()
        messagebox.showerror("文件存储错误", str(e))

    finally:
        return flag


def create_workbook_save(work_book_name: str, save_data,
                         top_row_list = None) -> bool:
    """
    新建工作簿写入,后续优化
    :param top_row_list: title行的数据
    :type top_row_list:list[str|int]
    :param save_data:数据二维数组
    :type save_data:list[list[]]
    :param work_book_name:工作簿名称
    :type work_book_name:str
    :return:保存成功 ? true : false
    :rtype:bool
    """
    flag = False
    try:
        wb = Workbook()

        ws = wb.active
        for data_list in save_data:
            ws.append(data_list)

        wb.save(work_book_name)
        flag = True
    except Exception as e:
        root = tkinter.Tk()
        root.withdraw()
        messagebox.showerror("文件存储错误", str(e))

    finally:
        return flag


def remove_sheet(sheet_name: str, file_path: str):
    flag = False
    try:
        wb = load_workbook(filename=file_path)
        wb.remove(sheet_name)
        flag = True
    except Exception as e:
        root = tkinter.Tk()
        root.withdraw()
        messagebox.showerror("文件删除错误!", str(e))
    finally:
        return flag


def move_sheet(sheet_name: str, file_path: str, index: str):
    flag = False
    try:
        wb = load_workbook(filename=file_path)
        wb.move_sheet(sheet_name, int(index))
        flag = True
    except Exception as e:
        root = tkinter.Tk()
        root.withdraw()
        messagebox.showerror("表格移动错误!", str(e))
    finally:
        return flag


def pandas_save_file_to_excel(df,file_name,sheet_name,is_index=True):
    flag = False
    try:
        save_file1 = file_name.replace("\\", "/")
        save_file1 = save_file1.replace("\\\\", "/")
        with pandas.ExcelWriter(save_file1, mode="a") as w:
            df.to_excel(w, sheet_name=sheet_name, index=is_index)
        flag = True
    except Exception as e:
        print(f"文件保存错误!错误信息为{e}")
    finally:
        return flag

